package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.ninetaildemonfox.zdl.socialproject.SpApplication;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.DHomeContract;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/6
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class DHomePresenter {
    public DHomeContract dHomeContract;
    private AppCompatActivity me;

    public DHomePresenter(DHomeContract dHomeContract) {
        this.dHomeContract = dHomeContract;
    }

    public DHomePresenter(DHomeContract dHomeContract, AppCompatActivity me) {
        this.dHomeContract = dHomeContract;
        this.me = me;
    }

    public void getIndex(final String token, final String sex, String lon, String lat, final String page,
                         final String type, String city, final String nickname) {
        if (TextUtils.isEmpty(token)) {
            return;
        }
        if (TextUtils.isEmpty(sex)) {
            ToastUtil.show("没有获取到性别");
            return;
        }
        if (city.equals("附近")) {
            Model.getInstance()
                    .getUserApi()
                    .getIndex(token,
                            sex, lon, lat,
                            page, type, "10000", nickname)
                    .enqueue(new Callback<JsonObject>() {
                        @Override
                        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                            Map<String, String> map = Constants.codeGet(me, response);
                            if (map == null) {
                                return;
                            }
                            ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                            dHomeContract.Success(list);
                        }

                        @Override
                        public void onFailure(Call<JsonObject> call, Throwable t) {

                        }
                    });
        } else {
            String finalLat = lat;
            String finalLon = lon;
            Model.getInstance().getUserApi().getQueryIdbyName(city).enqueue(new Callback<JsonObject>() {
                @Override
                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                    Map<String, String> map = Constants.codeGet(me, response);
                    if (map == null) {
                        return;
                    }
                    Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                    if (data == null) {
                        return;
                    }
                    Model.getInstance()
                            .getUserApi()
                            .getIndex(token,
                                    sex, finalLon, finalLat,
                                    page, type, data.get("id"), nickname)
                            .enqueue(new Callback<JsonObject>() {
                                @Override
                                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                                    Map<String, String> map = Constants.codeGet(me, response);
                                    if (map == null) {
                                        return;
                                    }
                                    ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                                    dHomeContract.Success(list);
                                }

                                @Override
                                public void onFailure(Call<JsonObject> call, Throwable t) {

                                }
                            });
                }

                @Override
                public void onFailure(Call<JsonObject> call, Throwable t) {

                }
            });
        }
    }

    public void getIndex2(final String token, final String sex, final String lon, final String lat, final String page,
                          final String type, final String city, final String nickname) {
        if (TextUtils.isEmpty(token)) {
            return;
        }
        if (TextUtils.isEmpty(sex)) {
            ToastUtil.show("没有获取到性别");
            return;
        }
        if (TextUtils.isEmpty(lon)) {
            ToastUtil.show("没有获取到经纬度");
            return;
        }
        Model.getInstance()
                .getUserApi()
                .getIndex(token,
                        sex, lon, lat,
                        "", "", "", nickname)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                        dHomeContract.Success(list);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }
}